treemap

Lecture des données :

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.5
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.0     v forcats 0.5.1
## Warning: package 'ggplot2' was built under R version 4.0.5
## Warning: package 'tibble' was built under R version 4.0.5
## Warning: package 'tidyr' was built under R version 4.0.5
## Warning: package 'readr' was built under R version 4.0.5
## Warning: package 'purrr' was built under R version 4.0.5
## Warning: package 'dplyr' was built under R version 4.0.5
## Warning: package 'stringr' was built under R version 4.0.5
## Warning: package 'forcats' was built under R version 4.0.5
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
flux <- read_csv("../data/obs_artif_conso_com_2009_2020_V2.csv", na = c("", "NULL"))
## Rows: 34938 Columns: 85
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr  (9): idcom, idcomtxt, idreg, idregtxt, iddep, iddeptxt, epci20txt, aav2...
## dbl (76): epci20, cateaav2020, naf09art10, art09act10, art09hab10, art09mix1...
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Palette 
colorBlue    <- hcl(h = 220, c = 50, l = 80, fixup = TRUE)
colorRed     <- hcl(h = 4, c = 50, l = 80, fixup = TRUE)
colorMagenta <- hcl(h = 300, c = 50, l = 80, fixup = TRUE)
colorGrey    <- hcl(h = 0, c = 0, l = 80, fixup = TRUE)

myPalette <- c("blue" = colorBlue,
                "red" = colorRed,
                "magenta" = colorMagenta,
                "grey" = colorGrey)
library(plotly)
## Warning: package 'plotly' was built under R version 4.0.5
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
makeTreemap <- function(flux, codeInsee) {
  df <- flux %>% filter(idcom == codeInsee)
  
  df <- df %>% gather("variable",
                      "value",
                      c("arthab0920", "artact0920", "artmix0920", "artinc0920"))
  
  df$variable <- case_when(
    df$variable == "arthab0920" ~ "Habitat",
    df$variable == "artact0920" ~ "Activité",
    df$variable == "artmix0920" ~ "Mixte",
    df$variable == "artinc0920" ~ "NC"
  )
  
  # Par. treemap
  labels = df$variable
  parents = rep("", nrow(df))
  values = df$value
  
  fig <- plot_ly(
    type="treemap",
    labels=labels,
    parents=parents,
    values=values,
    marker=list(colors = myPalette))
  
  fig
}

Faisons la treemap de la commune d’Aix en Provence :

flux %>% makeTreemap("13001")

Faisons la treemap sur Toulon :

flux %>% makeTreemap("83137")